<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>图书管理</title>
		<link rel="stylesheet" href="../../component/pear/css/pear.css" />
	</head>
	<!--检索组件-->
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">书名</label>
							<input type="hidden" name="bid">
							<div class="layui-input-inline">
								<input type="text" name="title" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">ISBN</label>
							<div class="layui-input-inline">
								<input type="text" name="isbn" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">作者</label>
							<div class="layui-input-inline">
								<input type="text" name="author" placeholder="" class="layui-input">
							</div>
						</div>

						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">状态</label>
							<div class="layui-input-inline">
								<select  name="surplusnum" placeholder="" class="layui-input">、
									<option value="-1">所有</option>
									<option value="f">有库存</option>
									<option value="0">已借完</option>

									</select>
							</div>
						</div>

						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="book-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<!--信息显示表格组件-->
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="book-table" lay-filter="book-table"></table>
			</div>
		</div>
		<!--添加、修改表单组件-->
		<div style="display: none" id="showBook">
			<form class="layui-form" action="" lay-filter="bookForm">
				<input type="hidden" name="bid">
				<div class="mainBox">
					<div class="main-container">
						<div class="main-container">
							<div class="layui-form-item">
								<label class="layui-form-label">书名</label>
								<div class="layui-input-block">
									<input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">ISBN</label>
								<div class="layui-input-block">
									<input type="text"  name="isbn" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
								</div>
							</div>

							<div class="layui-form-item">
								<label class="layui-form-label">出版社</label>
								<div class="layui-input-block">
									<input type="text"  name="publish" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">作者</label>
								<div class="layui-input-block">
									<input type="text"  name="author" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">总数</label>
								<div class="layui-input-block">
									<input type="text"  name="totalnum" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">类别</label>
								<div class="layui-input-block">
									<select name="btype">
										<option value="1"> 计算机类</option>
										<option value="2"> 文史类</option>
										<option value="3"> 社科类</option>
									</select>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">位置</label>
								<div class="layui-input-block">
									<select id="right" name="blid">
										<option value="1"> 1馆1号架</option>
										<option value="2"> 2馆2号架</option>
										<option value="3"> 3馆1号架</option>
									</select>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="bottom">
					<div class="button-container">
						<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="book-submit">
							<i class="layui-icon layui-icon-ok"></i>
							提交
						</button>
						<button type="reset" class="pear-btn pear-btn-sm">
							<i class="layui-icon layui-icon-refresh"></i>
							重置
						</button>
					</div>
				</div>
			</form>
		</div>

		<!--新增，批量删除组件-->
		<script type="text/html" id="book-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        添加新书
		    </button>
		    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        批量删除
		    </button>
		</script>
<!--修改删除组件-->
		<script type="text/html" id="book-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>

		<script type="text/html" id="book-createTime">
			{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
		</script>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;
				let MODULE_PATH = "operate/";
				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: '书名',
							field: 'title',
                            align: 'center'
						},
						{
							title: 'ISBN',
							field: 'isbn',
                            align: 'center'
						},
						{
							title: '出版社',
							field: 'publish',
                            align: 'center'
						},
						{
							title: '类别',
							field: 'btype',
                            align: 'center'
						},
						{
							title: '作者',
							field: 'author',
                            align: 'center'
						},
						{
							title: '位置信息',
							field: 'bloc',
                            align: 'center'
						},
						{
							title: '总数量',
							field: 'totalnum',
                            align: 'center'
						},
						{
							title: '剩余数量',
							field: 'surplusnum',
                            align: 'center'
						},

						{
							title: '操作',
							toolbar: '#book-bar',
                            align: 'center'
						}
					]
				]

				table.render({
					elem: '#book-table',
					url: 'http://localhost:8080/books/select',
					page: true,
					cols: cols,
					skin: 'line',
					limits: [5,10,15,25,30],
					toolbar: '#book-toolbar',
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});

				table.on('tool(book-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(book-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(book-query)', function(data) {
					table.reload('book-table', {
						where: data.field,
						page: {curr:1}
					})
					console.log(data)
					return false;
				});

				form.on('switch(book-enable)', function(obj) {
					layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
				});

				window.add = function() {
                    index = layer.open({
                        type: 1,
                        title: '新增图书',
                        area: ['500px', '600px'],
                        content: $('#showBook')
                    });
                    form.on('submit(book-submit)', function(data){
                    	let loading = layer.load(2)
                        //提交数据
                        $.post(
                                'http://localhost:8080/books/addOne',
                                data.field,
                                function(msg){
                                    layer.close(index);//关闭当前页
                                    table.reload("book-table");
                                    layer.close(loading);
                                    layer.msg("添加成功",{icon:6,time:2000})
                                }
                        )
						return false;
                    });
				}

				window.edit = function(obj) {
					let loading = layer.load(2);
					console.log(obj.data.bid);
                    $.get(
                    		"http://localhost:8080/books/selectOne",
							{bid:obj.data.bid},
							function (data){
								index = layer.open({
									type: 1,
									title: '修改图书',
									area: ['500px', '600px'],
									content: $('#showBook') //注意，如果str是object，那么需要字符拼接。
								});
								console.log(data)
								form.val('bookForm',data)
								layer.close(loading)
							}
					)

                    //form.val("addForm", obj1);//回显数据
                    //form.render('radio');
                    //});
                    form.on('submit(book-submit)', function(data){
                    	let loading = layer.load(2)
                        //提交数据
                        $.post(
                                'http://localhost:8080/books/updateOne',
                                data.field,
                                function(msg){
                                    layer.close(index);//关闭当前页
                                    table.reload("book-table");
                                    layer.close(loading);
                                    layer.msg("修改成功",{icon:6,time:2000})
                                }
                        )
						return false;
                    });
				}

				window.remove = function(obj) {
					layer.confirm('确定要删除该图书', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load(2);
						$.get(
								"http://localhost:8080/books/deleteOne",
								{bid:obj.data.bid},
								function (msg){
									table.reload("book-table")
									layer.close(loading)
									layer.msg("删除成功",{icon:6,time:2000})
								}
						)
					});
				}

				window.batchRemove = function(obj) {

					var data = table.checkStatus(obj.config.id).data;
					if (data === "") {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					let bids = "";
					for (i=0;i<data.length;i++){
						bids+= data[i].bid+",";
					}
					bids = bids.substr(0,bids.length-1)
					console.log(bids)
					layer.confirm('确定要删除这些图书', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.get(
								"http://localhost:8080/books/batchRemove",
								{bids:bids},
								function (msg){
									table.reload("book-table")
									layer.close(loading)
									layer.msg("删除成功",{icon:6,time:2000})
								}
						)
					});
				}

				window.refresh = function(param) {
					table.reload('book-table');
				}
			})
		</script>
	</body>
</html>
